package cn.wolfcode.rbac.controller;

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String listAll(Model model, QueryObject qo){
        PageResult pageResult = permissionService.query(qo);
        model.addAttribute("pageResult",pageResult);
        //把载具里面的数据传递给用户看
        //请求转发
        //return "forward:/WEB-INF/views/permission/list.jsp";
        return "permission/list";
    }

    @RequestMapping("/reload")
    public String reload(){
        // 调用service去加载权限
        permissionService.reload();
        return "redirect:/permission/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        permissionService.delete(id);
        //重定向,重新定位到一个新的请求路径
        return "redirect:/permission/list";
    }


    //跳转到input页面
    @RequestMapping("/input")
    public String input(Model model,Long id){
        if (id != null) {
            Permission one = permissionService.getOne(id);
            model.addAttribute("dept",one);
        }
        //请求转发
        return "permission/input";
    }


    //跳转到input页面之后,填完/修改完数据信息之后,点击保存按钮
    //把数据保存到数据库,然后重定向到 permission/list 请求路径,相当于又帮我们查了一次
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission){
        permissionService.saveOrUpdate(permission);
        //保存/修改之后, 重定向到permission/list请求路径,相当于重新查询了一次最新的数据
        return "redirect:/permission/list";
    }
}
